#ifndef DB_
#define DB_
#include "WProgram.h"
#include <stdint.h>

#define SD_MR_RECORDS 6
enum {NGC,MESSIER,NAMED_STARS,OBJECTS_IN_TILE,DSS2atlas,atlas};

struct master {
  char      name[24]; 
  uint32_t  offset;
  uint32_t  totalSize;   	  
  uint32_t  recordSize;
  uint32_t  numRecords;
  uint32_t  auxValue;
};


struct ngc {
  uint16_t ngc_n;
  uint16_t ngc_ic_messier;   	  
  float ra;
  float dec;
  float mag;
  uint16_t image;
  char  name[24];
  char  type[4];  
  uint16_t tile;   
};

struct star {
  uint32_t HIP;
  float ra;
  float dec;
  float mag;   
  uint16_t  F;
  char  bayer[5];  
  char costellation[3];
  char  name[18];  
};

struct objects {
  uint16_t tile;
  uint16_t stars_from;
  uint16_t stars_to;
  uint16_t ngc_from;
  uint16_t ngc_to;
  uint16_t messier_from;
  uint16_t messier_to;
};



struct DB_Header
{

  uint32_t n_recs;
  uint32_t rec_size;
  uint32_t table_size;

};

enum DB_Status { 
                          DB_OK,
                          DB_OUT_OF_RANGE,
                          DB_TABLE_FULL
                        };

typedef byte* DB_Rec;

#define DB_REC (byte*)(void*)&

class SD_DB 
{
  public:
    SD_DB();
    DB_Status DBcreate(uint32_t, uint32_t, uint16_t);
    DB_Status DBopen(uint32_t);
    DB_Status DBreadRec(uint32_t, DB_Rec);
    DB_Status DBdeleteRec(uint32_t);	
    DB_Status DBinsertRec(uint32_t, const DB_Rec);
    DB_Status DBupdateRec(uint32_t, const DB_Rec);
    DB_Status DBappendRec(DB_Rec rec);
    uint32_t DBlimit();
    uint32_t DBcount();
    void DBclear();
  private:
    virtual void DB_write_byte(uint32_t address, byte data) ;
    virtual byte DB_read_byte(uint32_t address) ;
    uint32_t DB_head_ptr;
    uint32_t DB_table_ptr;
    DB_Header DB_head;
    void DBWrite(uint32_t ee, const byte* p, uint16_t);
    void DBRead(uint32_t ee, byte* p, uint16_t);
    void DBwriteHead();
    void DBreadHead();
    DB_Status DBwriteRec(uint32_t, const DB_Rec);
};


#endif
